#pragma once
#include <iostream>
#include <string>
#include <fstream>

#include <json/json.h>
#include <boost/asio.hpp>
#include <boost/system.hpp>
#include <boost/thread.hpp>

#include "DBServer/db_grpc_client.h"
#include "JsonMessageParsing/json_message_parsing.h"


class tcp_server {
public:
	typedef boost::asio::ip::tcp::acceptor acceptor;
	typedef std::shared_ptr<acceptor>  acceptor_shared_ptr;

	typedef boost::asio::ip::tcp::socket socket;
	typedef std::shared_ptr<socket> socket_shared_ptr;

	typedef boost::asio::io_service io_service;
	typedef std::shared_ptr<io_service> io_service_shared_ptr;

	typedef boost::asio::ip::tcp::iostream iostream_socket;
	typedef boost::asio::ip::tcp::endpoint endpoint;

	typedef socket* socket_ptr;
protected:
	acceptor_shared_ptr acceptor_sp;
	io_service_shared_ptr io_service_sp;
	std::list<std::pair<std::string, socket_shared_ptr>> sessions;


	json_message_parsing jmp;
	Json::Value root;
	Json::Reader reader;

public:
	tcp_server();
	~tcp_server();

	void run();

	void accept();
	void accept_handler(const boost::system::error_code& ec,
		socket_shared_ptr sock);

	void read(socket_shared_ptr sock);
	void read_handler(const boost::system::error_code& error,
		std::size_t bytes_transferred,
		std::shared_ptr<std::vector<char>> buffer,
		socket_shared_ptr sock);

	void print_sessions();

	void print_tcp_server_message();

};